home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / optionwidgets.py < prev    next >
Text File  |  2009-10-19  |  8KB  |  224 lines

  1. ## system-config-printer
  2.  
  3. ## Copyright (C) 2006, 2007, 2008, 2009 Red Hat, Inc.
  4. ## Copyright (C) 2006 Florian Festi <ffesti@redhat.com>
  5. ## Copyright (C) 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import gtk.glade, cups
  22. from gettext import gettext as _
  23. import ppdippstr
  24.  
  25. def OptionWidget(option, ppd, gui, tab_label=None):
  26.     """Factory function"""
  27.     ui = option.ui
  28.     if (ui == cups.PPD_UI_BOOLEAN and
  29.         len (option.choices) != 2):
  30.         # This option is advertised as a Boolean but in fact has more
  31.         # than two choices.
  32.         print "Treating Boolean option %s as PickOne" % option.keyword
  33.         ui = cups.PPD_UI_PICKONE
  34.  
  35.     if ui == cups.PPD_UI_BOOLEAN:
  36.         return OptionBool(option, ppd, gui, tab_label=tab_label)
  37.     elif ui == cups.PPD_UI_PICKONE:
  38.         return OptionPickOne(option, ppd, gui, tab_label=tab_label)
  39.     elif ui == cups.PPD_UI_PICKMANY:
  40.         return OptionPickMany(option, ppd, gui, tab_label=tab_label)
  41.  
  42. # ---------------------------------------------------------------------------
  43.  
  44. class Option:
  45.     def __init__(self, option, ppd, gui, tab_label=None):
  46.         self.option = option
  47.         self.ppd = ppd
  48.         self.gui = gui
  49.         self.enabled = True
  50.         self.tab_label = tab_label
  51.  
  52.         vbox = gtk.VBox()
  53.         
  54.         self.btnConflict = gtk.Button()
  55.         icon = gtk.image_new_from_stock(gtk.STOCK_DIALOG_WARNING,
  56.                                         gtk.ICON_SIZE_SMALL_TOOLBAR)
  57.         self.btnConflict.add(icon)
  58.         self.btnConflict.set_no_show_all(True) #avoid the button taking
  59.                                                # over control again
  60.         vbox.add(self.btnConflict)    # vbox reserves space while button
  61.         #vbox.set_size_request(32, 28) # is hidden
  62.         self.conflictIcon = vbox
  63.  
  64.         self.btnConflict.connect("clicked", self.on_btnConflict_clicked)
  65.         icon.show()
  66.  
  67.         self.constraints = [c for c in ppd.constraints
  68.                             if (c.option1 == option.keyword or
  69.                                 c.option2 == option.keyword)]
  70.         #for c in self.constraints:
  71.         #    if not c.choice1 or not c.choice2:
  72.         #        print c.option1, repr(c.choice1), c.option2, repr(c.choice2)
  73.         self.conflicts = set()
  74.         self.conflict_message = ""
  75.  
  76.     def enable(self, enabled=True):
  77.         self.selector.set_sensitive (enabled)
  78.         self.enabled = enabled
  79.  
  80.     def disable(self):
  81.         self.enable (False)
  82.  
  83.     def is_enabled(self):
  84.         return self.enabled
  85.  
  86.     def get_current_value(self):
  87.         raise NotImplemented
  88.  
  89.     def is_changed(self):
  90.         return self.get_current_value()!= self.option.defchoice
  91.     
  92.     def writeback(self):
  93.         #print repr(self.option.keyword), repr(self.get_current_value())
  94.         if self.enabled:
  95.             self.ppd.markOption(self.option.keyword, self.get_current_value())
  96.  
  97.     def checkConflicts(self, update_others=True):
  98.         value = self.get_current_value()
  99.         for constraint in self.constraints:
  100.             if constraint.option1 == self.option.keyword:
  101.                 option2 = self.gui.options.get(constraint.option2, None)
  102.                 choice1 = constraint.choice1
  103.                 choice2 = constraint.choice2
  104.             else:
  105.                 option2 = self.gui.options.get(constraint.option1, None)
  106.                 choice1 = constraint.choice2
  107.                 choice2 = constraint.choice1
  108.  
  109.             if option2 is None: continue
  110.  
  111.             if (choice1==value and
  112.                 option2.get_current_value() == choice2):
  113.                 # conflict
  114.                 self.conflicts.add(constraint)
  115.                 if update_others:
  116.                     option2.checkConflicts(update_others=False)
  117.             elif constraint in self.conflicts:
  118.                 # remove conflict
  119.                 self.conflicts.remove(constraint)
  120.                 option2.checkConflicts(update_others=False)
  121.  
  122.  
  123.         tooltip = [_("Conflicts with:")]
  124.         for c in self.conflicts:
  125.             if c.option1 == self.option.keyword:
  126.                 option = self.gui.options.get(c.option2)
  127.             else:
  128.                 option = self.gui.options.get(c.option1)
  129.  
  130.             tooltip.append(option.option.text)
  131.             
  132.         tooltip = "\n".join(tooltip)
  133.  
  134.         self.conflict_message = tooltip # XXX more verbose
  135.             
  136.         if self.conflicts:
  137.             self.btnConflict.set_tooltip_text (tooltip)
  138.             self.btnConflict.show()
  139.         else:
  140.             self.btnConflict.hide()
  141.  
  142.         self.gui.option_changed(self)
  143.         return self.conflicts
  144.             
  145.     def on_change(self, widget):
  146.         self.checkConflicts()
  147.  
  148.     def on_btnConflict_clicked(self, button):
  149.         parent = self.btnConflict
  150.         while parent != None and not isinstance (parent, gtk.Window):
  151.             parent = parent.get_parent ()
  152.  
  153.         dialog = gtk.MessageDialog (parent,
  154.                                     gtk.DIALOG_DESTROY_WITH_PARENT |
  155.                                     gtk.DIALOG_MODAL,
  156.                                     gtk.MESSAGE_WARNING,
  157.                                     gtk.BUTTONS_CLOSE,
  158.                                     self.conflict_message)
  159.         dialog.run()
  160.         dialog.destroy()
  161.         
  162. # ---------------------------------------------------------------------------
  163.  
  164. class OptionBool(Option):
  165.  
  166.     def __init__(self, option, ppd, gui, tab_label=None):
  167.         self.selector = gtk.CheckButton(ppdippstr.ppd.get (option.text))
  168.         self.label = None
  169.         self.false = u"False" # hack to allow "None" instead of "False"
  170.         self.true = u"True"
  171.         for c in option.choices:
  172.             if c["choice"] in ("None", "False", "Off"):
  173.                 self.false = c["choice"]
  174.             if c["choice"] in ("True", "On"):
  175.                 self.true = c["choice"]
  176.         self.selector.set_active(option.defchoice == self.true)
  177.         self.selector.set_alignment(0.0, 0.5)
  178.         self.selector.connect("toggled", self.on_change)
  179.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  180.  
  181.     def get_current_value(self):
  182.         return (self.false, self.true)[self.selector.get_active()]
  183.  
  184. # ---------------------------------------------------------------------------
  185.  
  186. class OptionPickOne(Option):
  187.     widget_name = "OptionPickOne"
  188.  
  189.     def __init__(self, option, ppd, gui, tab_label=None):
  190.         self.selector = gtk.combo_box_new_text()
  191.         #self.selector.set_alignment(0.0, 0.5)
  192.  
  193.         label = ppdippstr.ppd.get (option.text)
  194.         if not label.endswith (':'):
  195.             label += ':'
  196.         self.label = gtk.Label(label)
  197.         self.label.set_alignment(0.0, 0.5)
  198.         
  199.         selected = None
  200.         for nr, choice in enumerate(option.choices):
  201.             self.selector.append_text(ppdippstr.ppd.get (choice['text']))
  202.             if option.defchoice == choice['choice']:
  203.                 selected = nr
  204.         if selected is not None:
  205.             self.selector.set_active(selected)
  206.         else:
  207.             print option.text, "unknown value:", option.defchoice
  208.         self.selector.connect("changed", self.on_change)
  209.  
  210.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  211.  
  212.     def get_current_value(self):
  213.         return self.option.choices[self.selector.get_active()]['choice']
  214.         
  215. # ---------------------------------------------------------------------------
  216.  
  217. class OptionPickMany(OptionPickOne):
  218.     widget_name = "OptionPickMany"
  219.  
  220.     def __init__(self, option, ppd, gui, tab_label=None):
  221.         raise NotImplemented
  222.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  223.         
  224.